//
//  FMDBManager.m
//  product_B
//
//  Created by lanou on 16/6/6.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "FMDBManager.h"
#import "ReadDetailCommentModel.h"
#import "CommentModel.h"
@implementation FMDBManager
+ (FMDBManager *)shareInstance{
    static FMDBManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[FMDBManager alloc]init];
    });
    return manager;
}

- (void)openDB{
    if (db != nil) {
        return;
    }
    NSString *filePath = [self createSqliteWithSqliteName:@"FMDB.sqlite"];
    db = [FMDatabase databaseWithPath:filePath];
//    NSLog(@"%@",filePath);
}
- (NSString *)createSqliteWithSqliteName:(NSString *)sqliteName{
    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:sqliteName];
}
- (void)createTableNameWithTableName:(NSString *)tableName{
    [db open];
    NSString *string = [NSString stringWithFormat:@"create table if not exists %@(ID integer primary key autoincrement, url text,content text, myID text, type text, input_date text, praisenum text,quote text,touser_name text,user_id text,user_name text,web_url text)",tableName];
    BOOL flag = [db executeUpdate:string];
    
    [db close];
}
- (void)insertReadCommentTableWithTableName:(NSString *)tableName url:(NSString *)url content:(NSString *)content myID:(NSString *)myID type:(NSNumber *)type input_date:(NSString *)input_date praisenum:(NSNumber *)praisenum quote:(NSString *)quote touser_name:(NSString *)touser_name user_id:(NSString *)user_id user_name:(NSString *)user_name web_url:(NSString *)web_url{
    [db open];
    NSString *string = [NSString stringWithFormat:@"insert into %@ (url, content, myID, type, input_date,praisenum,quote,touser_name,user_id,user_name,web_url) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",tableName];
    //  不支持传入非对象性参数 如果是UIImage  如果直接传入image 会是以data的形式存储  如果是UIImagePNGRepresentation 在数据库内部能够直观的看到图片
    
    BOOL flag = [db executeUpdate:string,url,content,myID,[NSString stringWithFormat:@"%@",type],input_date,[NSString stringWithFormat:@"%@",praisenum],quote,touser_name,user_id,user_name,web_url];
//    NSLog(@"插入数据 == %d",flag);
    [db close];
}
- (void)deleteReadCommentTableWithTableName:(NSString *)tableName forURL:(NSString *)URL myID:(NSString *)myID{
    [db open];
    NSString *string = [NSString stringWithFormat:@"delete from %@ where url = ? and myID <> ?",tableName];
    BOOL flag = [db executeUpdate:string,URL,myID];
//    NSLog(@"删除数据 === %d",flag);
    [db close];
}
- (NSMutableArray *)selectReadCommentTableWithTableName:(NSString *)tableName forURL:(NSString *)URL type:(NSNumber *)type{
    [db open];
    NSMutableArray *modelArray = [NSMutableArray array];
    NSString *string = [NSString stringWithFormat:@"select * from %@ where url = ? and type = ?",tableName ];
    FMResultSet *result = [db executeQuery:string,URL,[NSString stringWithFormat:@"%@",type]];
    
    while ([result next]) {
        ReadDetailCommentModel *model = [[ReadDetailCommentModel alloc]init];
        model.content = [result stringForColumn:@"content"];
        model.myID = [result stringForColumn:@"myID"];
        model.type = [NSNumber numberWithInt:[result stringForColumn:@"type"].intValue];
        model.input_date = [result stringForColumn:@"input_date"];
        model.quote = [result stringForColumn:@"quote"];
        if ([result stringForColumn:@"touser_name"]) {
            model.touser = [NSMutableDictionary dictionary];
        }
        [model.touser setValue:[result stringForColumn:@"touser_name"] forKey:@"user_name"];
        
        model.user = [@{@"user_id":[result stringForColumn:@"user_id"],@"user_name":[result stringForColumn:@"user_name"],@"web_url":[result stringForColumn:@"web_url"]}mutableCopy];
        model.praisenum = [NSNumber numberWithInt:[result stringForColumn:@"praisenum"].intValue];
        [modelArray addObject:model];
    }
    if (type.intValue == 1) {
        [modelArray sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            ReadDetailCommentModel *model1 = (ReadDetailCommentModel *)obj1;
            ReadDetailCommentModel *model2 = (ReadDetailCommentModel *)obj2;
            return [model2.input_date compare:model1.input_date];
        }];
    }
    return modelArray;
}
- (NSMutableArray *)selectMusicCommentTableWithTableName:(NSString *)tableName forURL:(NSString *)URL type:(NSNumber *)type{
    [db open];
    NSMutableArray *modelArray = [NSMutableArray array];
    NSString *string = [NSString stringWithFormat:@"select * from %@ where url = ? and type = ?",tableName ];
    FMResultSet *result = [db executeQuery:string,URL,[NSString stringWithFormat:@"%@",type]];
    
    while ([result next]) {
        CommentModel *model = [[CommentModel alloc]init];
        model.content = [result stringForColumn:@"content"];
        model.myID = [result stringForColumn:@"myID"];
        model.type = [NSNumber numberWithInt:[result stringForColumn:@"type"].intValue];
        model.input_date = [result stringForColumn:@"input_date"];
        model.quote = [result stringForColumn:@"quote"];
        if ([result stringForColumn:@"touser_name"]) {
            model.touser.user_name = [result stringForColumn:@"touser_name"];
        }
        model.user.user_id = [result stringForColumn:@"user_id"];
        model.user.user_name = [result stringForColumn:@"user_name"];
        model.user.web_url = [result stringForColumn:@"web_url"];
        model.praisenum = [NSNumber numberWithInt:[result stringForColumn:@"praisenum"].intValue];
        [modelArray addObject:model];
    }
    if (type.intValue == 1) {
        [modelArray sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            ReadDetailCommentModel *model1 = (ReadDetailCommentModel *)obj1;
            ReadDetailCommentModel *model2 = (ReadDetailCommentModel *)obj2;
            return [model2.input_date compare:model1.input_date];
        }];
    }
    return modelArray;
}
@end
